package com.hualan.dao;

import com.hualan.bean.PanghuCourierStation;
import com.hualan.dao.StageDao1;
import com.hualan.Util.DBUtils;
import com.hualan.Util.JdbcUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class StageDaoImpl1 implements StageDao1 {

    // 搜索
    @Override
    public List<PanghuCourierStation> search(String pageNum, String pageSize, String courierStationName, String communityName, String courierStationStatus, String startTime, String endTime) {

        QueryRunner qr = new QueryRunner(JdbcUtil.dataSource);
        List<PanghuCourierStation> query = null;
        List<String> ls = new ArrayList<>();
        StringBuilder sql = new StringBuilder("select courier_station_id courierStationId," + "courier_station_name courierStationName," + "courier_station_image courierStationImage," + "courier_station_province courierStationProvince," + "courier_station_city courierStationCity," + "courier_station_district courierStationDistrict," + "courier_station_address courierStationAddress," + "courier_station_status courierStationStatus," + "add_time addTime," + "courier_station_sort courierStationSort," + "courier_station_community_id courierStationCommunityId from panghu_courier_station left join panghu_community on courier_station_community_id = community_id where 1 = 1");
        if (courierStationName != null && courierStationName.trim().length() != 0){
            sql.append(" and courier_station_name like ?");
            ls.add("%" + courierStationName + "%");
        }
        if (communityName != null && communityName.trim().length() != 0){
            sql.append(" and community_name like ?");
            ls.add("%" + communityName + "%");
        }
        if (startTime != null && startTime.trim().length() != 0){
            sql.append(" and add_time > ?");
            ls.add(startTime);
        }
        if (endTime != null && endTime.trim().length() != 0){
            sql.append(" and add_time < ?");
            ls.add(endTime);
        }
        Object[] objects = ls.toArray();
        try {
            if (objects.length != 0){
                query = qr.query(sql.toString(), new BeanListHandler<>(PanghuCourierStation.class), objects);
            }else {
                query = qr.query(sql.toString(), new BeanListHandler<>(PanghuCourierStation.class));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return query;

    }

    @Override
    public int save(PanghuCourierStation pcs) {
        int num = DBUtils.update("insert into panghu_courier_station (courier_station_name, courier_station_province, courier_station_city, courier_station_district, courier_station_address) values (?,?,?,?,?)", pcs.getCourierStationName(), pcs.getCourierStationProvince(), pcs.getCourierStationCity(), pcs.getCourierStationDistrict(), pcs.getCourierStationAddress());
        return num;
    }


    @Override
    public int modify(int id,int status){
        int row = 0;
        String sql = "update panghu_courier_station set courier_station_status = ? where courier_station_id = ?";
        row = DBUtils.update(sql, status, id);
        return row;
    }

    @Override
    public Long selectAllPanghuCourierStationCount() {
        Map<String, Object> map= DBUtils.queryOne("select count(*) as count from  panghu_courier_station ");
        Long count = (Long) map.get("count");
        return count;
    }

    @Override
    public List<PanghuCourierStation> selectPanghuCourierStationWithPage(int page, int size) {
        List<PanghuCourierStation> bookList = DBUtils.query("select * from panghu_courier_station limit ?,?", PanghuCourierStation.class, (page - 1) * size, size);
//        for (PanghuCourierStation book : bookList) {
//            System.out.println(book);
//        }
        return bookList;
    }





    //编辑驿站
    @Override
    public Integer updateStage(PanghuCourierStation yz) {
        Integer row = DBUtils.update("update panghu_courier_station set courier_station_name=? ,courier_station_image=?,courier_station_province=?,courier_station_city=?,courier_station_district=?,courier_station_address=?,courier_station_status=? where courier_station_id =?",yz.getCourierStationName(),yz.getCourierStationImage(),yz.getCourierStationProvince(),yz.getCourierStationCity(),yz.getCourierStationDistrict(),yz.getCourierStationAddress(),yz.getCourierStationStatus(),yz.getCourierStationId() );
        return row;


    }


}



/*join panghu_community as pc on pcs.courier_station_community_id = pc.community_id*/